package com.zl.controller;

import com.alibaba.fastjson.JSON;
import com.zl.controller.annotation.First;
import com.zl.controller.annotation.Second;
import com.zl.controller.annotation.UserGroup;
import com.zl.model.MyValidateUser;
import com.zl.model.MyValidateUser2;
import com.zl.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.GroupSequence;
import javax.validation.Valid;
import java.security.acl.Group;
import java.util.List;

/**
 * @author ZLL
 * @date create in 2018/04/26
 */
@Controller
public class MyValidateController {


    @RequestMapping("/save")
    @ResponseBody
    public String save(@Validated MyValidateUser user, BindingResult result) {

        System.out.println(1111111111);
        System.out.println(JSON.toJSONString(user));
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError error : allErrors) {
                System.out.println(error.getDefaultMessage());
            }
        }
        return "haha";
    }


    @RequestMapping("/save2")
    @ResponseBody
    public String save2(@Validated({Second.class}) MyValidateUser user, BindingResult result) {

        System.out.println(22222222);
        System.out.println(JSON.toJSONString(user));
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError error : allErrors) {
                System.out.println(error.getDefaultMessage());
            }
        }
        return "haha";
    }


    @RequestMapping("/save3")
    @ResponseBody
    public String save3(@Validated(UserGroup.class) MyValidateUser2 user, BindingResult result) {

        System.out.println(33333);
        System.out.println(JSON.toJSONString(user));
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError error : allErrors) {
                System.out.println(error.getDefaultMessage());
            }
        }
        return "haha3";
    }


    @RequestMapping("/save4")
    @ResponseBody
    public String save4(@Validated User user, BindingResult result) {

        System.out.println(4444444);
        System.out.println(JSON.toJSONString(user));
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError error : allErrors) {
                System.out.println(error.getDefaultMessage());
            }
        }
        return "haha4";
    }
}
